angular.module('Caltech').controller('certificateController', function ($scope, Restangular, ngTableParams, $stateParams, $location, Constants) {
    'use strict';

    $scope.customer = { name: '' };
    $scope.filters = {};

    $scope.account = Constants.accountInfo;

    $scope.paging = {
        index: 1,
        size: 10
    };

    $scope.filter = false;

    $scope.checkHideCustomer = function () {
        if ($scope.account.roleName == 'Customer')
            return false;
        return true;
    };

    $scope.getCustomerList = function (nameFitler) {
        return Restangular.all('customer/list').getList({
            name: nameFitler
        });
    };



    $scope.search = function (customer) {
        if (typeof (customer) === 'string') {
            $scope.customer = {
                name: customer
            }
        } else if (typeof (customer) === 'object') {
            $scope.customer = customer;
        }

        if ($scope.tableParams) {
            $scope.tableParams.reload();
        } else {
            $scope.tableParams = new ngTableParams({
                page: $scope.paging.index,
                count: $scope.paging.size
            }, {
                counts: [20, 30, 40],
                getData: function ($defer, params) {
                    var customerId = $scope.customer.id || undefined,
                        customerName = customerId === undefined && $scope.customer.name ? $scope.customer.name : undefined;
                    var username = $scope.account.roleName == 'Customer' ? $scope.account.username : undefined;

                    if ($scope.filter) {
                        $scope.filter = false;
                        params.page(1);
                    }

                    Restangular.all('transaction')
                        .getList({
                            page: params.page(),
                            count: params.count(),
                            customerName: customerName,
                            customerId: customerId,
                            serialNumber: $scope.filters.serialNo || undefined,
                            certificateNumber: $scope.filters.certificateNo || undefined,
                            modelNumber: $scope.filters.modelNo || undefined,
                            workOrderNo: $scope.filters.workOrderNo || undefined,
                            username: username
                        }).then(function (quotes) {
                            params.total(quotes.total);
                            $defer.resolve(quotes);

                            $location.search('customerName', $scope.customer.name || undefined);
                            $location.search('customerId', $scope.customer.id);
                            $location.search('serialNo', $scope.filters.serialNo || undefined);
                            $location.search('certificateNo', $scope.filters.certificateNo || undefined);
                            $location.search('modelNo', $scope.filters.modelNo || undefined);
                            $location.search('workOrderNo', $scope.filters.workOrderNo || undefined);
                            if (params.page() != $scope.paging.index) {
                                $location.search('pageIndex', params.page());
                                $scope.paging.index = params.page();
                            }
                            if (params.count() != $scope.paging.size) {
                                $location.search('pageIndex', 1);
                                $location.search('pageSize', params.count());
                                $scope.paging.size = params.count();
                            }
                        });
                }
            });
        }
    };

    $scope.updateCustomer = function (item) {
        $location.search('pageIndex', 1);
        $scope.paging.index = 1;
        $scope.filter = true;
        $scope.search({ id: item.id, name: item.name });
    };

    $scope.reSearch = function (customer) {
        $location.search('pageIndex', 1);
        $scope.paging.index = 1;
        $scope.filter = true;
        $scope.search(customer);
    };


    if ($stateParams.pageIndex || $stateParams.customerId || $stateParams.customerName || $stateParams.serialNo || $stateParams.certificateNo || $stateParams.pageSize) {
        $scope.filters.serialNo = $stateParams.serialNo || undefined;
        $scope.filters.certificateNo = $stateParams.certificateNo || undefined;
        $scope.paging = {
            index: $stateParams.pageIndex || 1,
            size: $stateParams.pageSize || 10
        };

        $scope.search(
            {
                id: $stateParams.customerId,
                name: $stateParams.customerName
            });
    } else {
        $scope.search();
    }
});